//
//  RegisterViewController.m
//  Bmob
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UITextField *confirmPassWord;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:169/255.0 green:199/255.0 blue:137/255.0 alpha:1];
    
}
- (IBAction)registerAction:(id)sender {
    
    if (self.userName.text.length == 0) {
        
        [self showAlertViewWithMessage:@"请输入用户名"];
        return;
    }else if (self.userName.text.length <= 6){
        
        [self showAlertViewWithMessage:@"账号至少七位"];
        return;
    }
    
    if (self.passWord.text.length == 0 || self.confirmPassWord.text.length == 0) {
        
        [self showAlertViewWithMessage:@"请输入密码"];
        return;
    }else if (self.passWord.text.length <=6 || self.confirmPassWord.text.length <= 6){
        
        [self showAlertViewWithMessage:@"密码不得少于七位"];
        return;
    }
    if (![self.passWord.text isEqualToString:self.confirmPassWord.text]) {
        
        [self showAlertViewWithMessage:@"两次密码不一致，请重新输入"];
        return;
    }

    BmobUser *bUser = [[BmobUser alloc]init];
    bUser.username = self.userName.text;
    bUser.password = self.passWord.text;
    
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        
        if (isSuccessful){
            
            NSLog(@"注册成功");
            [self showAlertViewWithMessage:@"注册成功"];
//            [self dismissViewControllerAnimated:YES completion:NULL];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            
            NSLog(@"注册失败可能有重复用户，错误信息：%@",error);
            [self showAlertViewWithMessage:@"注册失败"];
        }
    }];
    
}
//创建弹窗
- (void)showAlertViewWithMessage:(NSString *)message{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
    
    [alertView show];
}

- (IBAction)cancel:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
